package com.zhl.tzsb;

import org.junit.Test;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: demos
 * @description:
 * @author: 刘振华
 * @create: 2020-11-16 13:35
 **/
public class BasicInfo {

	private JspReader jr = new JspReader();
	Map<String,List<String>> map = new HashMap<>(20);
	Map<String, String> action2Table = jr.getFile();


	@Test
	public void getBoiler() {

//		boiler();

//		vessel();

//		elec();

//		safety();

//		gas();

//		oilvessel();

//		heater();

		breather();
	}

	//呼吸器
	public void breather(){
		String basicPath = "breather";
		Map<String,String> jsp2Comment = new HashMap<>(5);

		jsp2Comment.put("breathervalve-management-base.jsp ","基本情况");
		jsp2Comment.put("breathervalve-management-attachment.jsp ","附件信息");
		jsp2Comment.put("breathervalve-followinspect.jsp ","历史检验信息");
		jsp2Comment.put("breathervalve-management-document.jsp ","资料文件");

		out2File(basicPath,jsp2Comment);
	}
	//加热炉
	public void heater(){
		String basicPath = "heater";
		Map<String,String> jsp2Comment = new HashMap<>(5);

		jsp2Comment.put("heater-management-base.jsp ","基本情况");
		jsp2Comment.put("heater-management-technical.jsp ","技术参数");
		jsp2Comment.put("heater-management-safety-appurtenances.jsp ","安全附件");
		jsp2Comment.put("heater-followinspect.jsp ","历史检验信息");
		jsp2Comment.put("heater-management-document.jsp ","资料文件");

		out2File(basicPath,jsp2Comment);
	}
	//油专容器
	public void oilvessel(){
		String basicPath = "oilvessel";
		Map<String,String> jsp2Comment = new HashMap<>(12);

		jsp2Comment.put("oil-vessel-management-base.jsp ","基本信息");
		jsp2Comment.put("oil-vessel-management-technical.jsp ","技术特性");
		jsp2Comment.put("oil-vessel-management-other.jsp ","其它信息");
		jsp2Comment.put("oil-vessel-management-conntube.jsp ","接管信息");
		jsp2Comment.put("oil-vessel-management-part.jsp ","零件信息");
		jsp2Comment.put("oil-vessel-management-safety-appurtenances.jsp ","安全附件");
		jsp2Comment.put("oil-vessel-followinspect.jsp ","历史检验信息");
		jsp2Comment.put("oil-vessel-management-quality.jsp ","质保文件");
		jsp2Comment.put("oil-vessel-management-builtdrawings.jsp ","竣工图");
		jsp2Comment.put("oil-vessel-management-weldmap.jsp ","焊缝图");
		jsp2Comment.put("oil-vessel-management-operrules.jsp ","操作规程");
		jsp2Comment.put("oil-vessel-management-document.jsp ","其他资料");





		out2File(basicPath,jsp2Comment);
	}
	//气瓶
	public void gas(){
		String basicPath = "gas";
		Map<String,String> jsp2Comment = new HashMap<>(10);
		jsp2Comment.put("gas-cylinder-management-base.jsp ","基本信息");
		jsp2Comment.put("gas-cylinder-management-technical.jsp ","技术参数");
		jsp2Comment.put("gas-cylinder-followinspect.jsp ","历史检验信息");
		jsp2Comment.put("gas-cylinder-management-document.jsp ","资料文件");




		out2File(basicPath,jsp2Comment);
	}
	//安全附件
	public void safety(){
		String basicPath = "safety";
		Map<String,String> jsp2Comment = new HashMap<>(10);
		jsp2Comment.put("safety-appurtenances-management-base.jsp ","基本信息");
		jsp2Comment.put("safety-appurtenances-management-technical.jsp ","技术特性");
		jsp2Comment.put("safety-appurtenances-management-document.jsp ","资料文件");



		out2File(basicPath,jsp2Comment);
	}
	//机电类
	public void elec(){
		String basicPath = "special";
		Map<String,String> jsp2Comment = new HashMap<>(10);
		jsp2Comment.put("special-equipment-management-base.jsp","基本信息");
		jsp2Comment.put("special-equipment-management-other.jsp","其他信息");
		jsp2Comment.put("special-equipment-management-technical.jsp","技术参数");
		jsp2Comment.put("special-equipment-management-empty.jsp","附图");
		jsp2Comment.put("special-equipment-followinspect.jsp","历史检验信息");
		jsp2Comment.put("special-equipment-management-document.jsp","资料文件");


		out2File(basicPath,jsp2Comment);
	}

	//压力容器
	public void vessel(){
		String basicPath = "vessel";
		Map<String,String> jsp2Comment = new HashMap<>(16);
		jsp2Comment.put("vessel-management-base.jsp","基本信息");
		jsp2Comment.put("vessel-management-technical.jsp","技术特性");
		jsp2Comment.put("vessel-management-other.jsp","其它信息");
		jsp2Comment.put("vessel-management-conntube.jsp","接管信息");
		jsp2Comment.put("vessel-management-part.jsp","零件信息");
		jsp2Comment.put("vessel-management-safety-appurtenances.jsp","安全附件");
		jsp2Comment.put("vessel-followinspect.jsp","历史检验信息");
		jsp2Comment.put("vessel-management-quality.jsp","质保文件");
		jsp2Comment.put("vessel-management-builtdrawings.jsp","竣工图");
		jsp2Comment.put("vessel-management-weldmap.jsp","焊缝图");
		jsp2Comment.put("vessel-management-livepicture.jsp","现场照片");
		jsp2Comment.put("vessel-management-vesseloperrules.jsp","操作规程");
		jsp2Comment.put("vessel-management-document.jsp","其他资料");

		out2File(basicPath,jsp2Comment);
	}

	//加热器
	public void boiler(){
		String basicPath = "boiler";
		Map<String,String> jsp2Comment = new HashMap<>(16);
		jsp2Comment.put("boiler-management-base.jsp","基本信息");
		jsp2Comment.put("boiler-management-technical.jsp","技术特性");
		jsp2Comment.put("boiler-management-other.jsp","其它信息");
		jsp2Comment.put("boiler-management-safety-appurtenances.jsp","安全附件");
		jsp2Comment.put("boiler-management-empty.jsp","附图");
		jsp2Comment.put("boiler-followinspect.jsp","历史检验信息");
		jsp2Comment.put("boiler-management-document.jsp","资料文件");
		jsp2Comment.put("boiler-management-drum.jsp ","圆筒规范");
		jsp2Comment.put("boiler-management-head.jsp ","封头规范");
		jsp2Comment.put("boiler-management-betwpipe.jsp ","接管座规范");
		jsp2Comment.put("boiler-management-btube.jsp ","机械性能");
		jsp2Comment.put("boiler-management-bunitescase.jsp ","联箱规格材质");
		jsp2Comment.put("boiler-management-bheatedsurface.jsp ","受热面及炉管规范");


		out2File(basicPath,jsp2Comment);
	}

	protected void out2File(String basicPath,Map<String,String> jsp2Comment){
		String destPath = "D:\\work\\workspace\\demos\\web-demo\\tzsb\\result\\"+basicPath;

		new File(destPath).mkdirs();


		String jspPath = getJspPath(basicPath);
		String nonFile = "不存在的文件为：";
		for(String key:jsp2Comment.keySet()){
			String tPath = jspPath+key;
			File file = new File(tPath);
			if(!file.exists()){
				nonFile += tPath+",";
				continue;
			}

			FileReader fr = null;
			BufferedReader bfr = null;
			try{
				fr = new FileReader(file);
				bfr = new BufferedReader(fr);
				String line = "";
				boolean flag = false;
				String tableName = "";

				List<String> list = null;
				while ((line = bfr.readLine())!=null){
					if(line.trim().contains("</form>")){
						break;
					}
					if(line.contains("method=\"post\"") && line.contains("action=\"${ctx}")){
						String[] strs = line.trim().split(".action\"")[0].split("/");
						tableName = action2Table.get(jr.getBigName(strs[strs.length-1].split("!")[0])+
							"Action")+","+jsp2Comment.get(key);
						if(map.containsKey(tableName)){
							list = map.get(tableName);
						}else{
							list = new ArrayList<>();
							map.put(tableName,list);
						}
						flag = true;
						continue;
					}
					if(flag) {
						line = line.trim();
						if (line.startsWith("<p ")) {
							try {
								String name = line.split("<label>")[1].split("</label>")[0];
								String comment = line.split("name=\"")[1].split("\"")[0];
								list.add(name + "," + comment.toUpperCase());
							}catch (Exception e){
								System.out.println(tPath);
								System.out.println(line);
								e.printStackTrace();
							}
						}
					}
				}
			}catch (Exception e){
				e.printStackTrace();
			}finally {
				if(null != fr) {
					try {
						fr.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
				if(null != bfr) {
					try {
						bfr.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		}

		map.forEach((k,v)->{
			File file = new File(destPath+File.separator+k+".csv");
			if(file.exists()){
				file.delete();
			}
			FileWriter fr = null;
			BufferedWriter bfw = null;
			try {
				file.createNewFile();
				fr = new FileWriter(file);
				bfw = new BufferedWriter(fr);
				for(int i = 0;i<v.size();i++){
					bfw.write(v.get(i));
					bfw.newLine();
				}
				bfw.flush();
				bfw.close();
			} catch (IOException e) {
				e.printStackTrace();
			}finally {
				if(null != fr) {
					try {
						fr.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
				if(null != bfw) {
					try {
						bfw.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		});
		System.out.println("======不存在的jsp===============");
		System.out.println(nonFile);
	}

	protected String getJspPath(String basic){
		return "D:\\work\\workspace\\tlm\\tzsb\\web\\WEB-INF\\content\\main"+
			File.separator+basic+ File.separator+"base\\card\\";
	}


}
